package com.qidian.mes.produce.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.qidian.common.pojo.MESResult;
import com.qidian.common.util.JSONTools;
import com.qidian.mes.pojo.Operate;
import com.qidian.mes.produce.service.OperateService;
import com.qidian.mes.produce.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * @Author 陈俊成
 * @Company ccc
 * @Description
 * @Time 2024/10/17 16:50
 */
@RestController
@RequestMapping("/operate")
@CrossOrigin
public class OperateController {
    @Autowired
    OperateService operateService;

    @GetMapping("/getByRelId")
    public MESResult queryAll(Integer operateProcessRelId, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int limit){
        System.out.println("wwqewqdssssssss"+operateProcessRelId);
        return operateService.queryAllByRelId(operateProcessRelId,page,limit);
    }

    @PostMapping("/addOperate")
    public MESResult addOperate(@RequestBody Operate operate){
        return operateService.addOperate(operate);

    }

    @GetMapping("/getPrimaryOperate")
    public MESResult getPrimaryOperate(int operateRelId){
        return operateService.queryOneByRel(operateRelId);
    }

    @PutMapping("/primaryOperate")
    public MESResult primaryOperate(@RequestBody Operate operate){
        System.out.println("ssssssss"+operate);
        return operateService.modifyOperate(operate);
    }

    @DeleteMapping("/deleteOne")
    public MESResult deleteOperate(int operateRelId){
        return operateService.deleteOperate(operateRelId);
    }

    @PostMapping("/deleteChoose")
    public MESResult deleteChooseOperate(@RequestBody List<Operate> operates){
        return operateService.deleteChoose(operates);
    }

    @GetMapping("/export")
    public void export(Integer operateProcessRelId,@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int limit, HttpServletResponse response) throws IOException {
        System.out.println("ssswww"+operateProcessRelId);
        MESResult result = operateService.queryAllByRelId(operateProcessRelId,page,limit);
        PageInfo<Operate> pageInfo = (PageInfo<Operate>) result.getData();
        List<Operate> operates = pageInfo.getList();
        try {
            // 准备输出数据
            // 设置contentType为excel的格式
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("工序步骤-" + System.currentTimeMillis(), "UTF-8").replaceAll("\\+", "%20");
            // 让文件以附件的形式下载
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 导出文件 , 这里的第一个参数就是一个输出流，这个流是通过response获取的指向客户端的输出流
            // 这个流不需要你关闭
            EasyExcel.write(response.getOutputStream(),Operate.class)
                    .sheet("工序步骤数据")
                    .doWrite(operates);
        } catch (Exception e) {
            if (!response.isCommitted()) {
                response.reset();
                response.setContentType("application/json");
                response.setCharacterEncoding("utf-8");
                response.getWriter().println(JSONTools.object2json(MESResult.error("下载文件失败")));
            }
        }



    }




}
